//
//  WBTabBarController.swift
//  WeiBo
//
//  Created by lihaixing on 2018/11/29.
//  Copyright © 2018年 lihaixing. All rights reserved.
//

import UIKit

class WBTabBarController: UITabBarController {
    private lazy var composeButton : UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildController()
        setupComposeButton()
        print(composeButton)
        

        // Do any additional setup after loading the view.
    }
    
//    private 保证方法私有，仅在当前对象内被访问
//    @objc 允许函数在运行时通过oc的消息机制被调用
    
    @objc private func composeBtnClicked() {
        print("撰写微博")
        
        let vc=UIViewController()
        let nav=UINavigationController.init(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
//    在代码中设置屏幕方向，保证所有自控制器都是portrait模式。
//    若需要对播放视频进行单独控制，用modal的方式展现
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}


extension WBTabBarController{
    
    private func setupComposeButton(){
        tabBar.addSubview(composeButton)
        
        let count=CGFloat(childViewControllers.count)
        let w=tabBar.bounds.width/count-1;//-1为了容错
        composeButton.frame=tabBar.bounds.insetBy(dx: 2*w, dy: 0)
        composeButton .addTarget(self, action:#selector(composeBtnClicked), for: .touchUpInside)
        print(tabBar.bounds)
        print(tabBar.frame)
    }

    fileprivate func setupChildController (){
        let array = [["clsName":"WBHomeController","title":"首页","imageName":"home"],["clsName":"WBMessageController","title":"消息","imageName":"message_center"],["claName":"UIViewController"],["clsName":"WBDiscoverController","title":"发现","imageName":"discover"],["clsName":"WBMineController","title":"我的","imageName":"profile"],]
        var arrM=[UIViewController]()
        for dic in array{
            arrM.append(createController(dict: dic))
        }
        viewControllers=arrM
        
    }
    
    fileprivate func createController(dict:[String:String])->UIViewController{
         guard let clsName = dict["clsName"],
            let imageName=dict["imageName"],
            let title=dict["title"],
        let cls=NSClassFromString(Bundle.main.namespace + "."+clsName) as?UIViewController.Type else {
                return WBBaseViewController()
                
                
        }
        let vc=cls.init()
        vc.title=title
        vc.tabBarItem.image=UIImage.init(named: "tabbar_"+imageName )?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage=UIImage.init(named: "tabbar_"+imageName+"_selected")?.withRenderingMode(.alwaysOriginal)
        let nav = WBNavigationController.init(rootViewController: vc)
        
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 12)], for: UIControlState.normal)
        vc.tabBarItem .setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.orange], for: UIControlState.highlighted)
        return nav;
        
        
        
    }
    
    
}
